package com.aranandroid.customview.dialog

import android.content.Context
import android.view.View
import android.widget.TextView

class DialogUtils private constructor() {
    /**
     * 两个按钮，两个回调
     *
     * @param context
     * @param string
     * @param listenerno
     * @param listeneryes
     */
    fun showDialog(
        context: Context?,
        string: String?,
        confirmListenter: ((view: View,dialog:CustomDialog) -> Unit)?,
        cancleListenter: ((view: View,dialog:CustomDialog) -> Unit)?,
        title:String = "提示"
        ) {
        val builder = CustomDialog.Builder(
            context!!
        )
        val customDialog = builder.dialogLayoutId(R.layout.dialog_prompt).title(title)
            .confirmListenter{ v,dialog ->
                confirmListenter?.let { it(v,dialog) }
            }
            .cancelListenter { v,dialog ->
                cancleListenter?.let { it(v,dialog) }
            }
            .build()
        (customDialog.childView!!.findViewById<View>(R.id.tv_remark) as TextView).setText(string)
        customDialog.show()
    }

    /**
     * 一个按钮，一个回调
     *
     * @param context
     * @param string
     * @param listeneryes
     */
    fun showDialog(
        context: Context?,
        string: String?,
        confirmListenter: ((view: View,dialog:CustomDialog) -> Unit)?,
        title:String = "提示"
    ) {
        val builder = CustomDialog.Builder(
            context!!
        )
        val customDialog =
            builder.dialogLayoutId(R.layout.dialog_prompt).title(title).confirmnoly(true).confirmListenter { v,dialog ->
                confirmListenter?.let { it(v,dialog) }
            }.build()
        (customDialog.childView!!.findViewById<View>(R.id.tv_remark) as TextView).setText(string)
        customDialog.show()
    }

    /**
     * 无按钮，无回调
     *
     * @param context
     * @param string
     */
    fun showDialog(context: Context?, string: String?,
                   title:String = "提示") {
        val builder = CustomDialog.Builder(
            context!!
        )
        val customDialog = builder.dialogLayoutId(R.layout.dialog_prompt).btShow(false).title(title).build()
        (customDialog.childView.findViewById<View>(R.id.tv_remark) as TextView).setText(string)
        customDialog.show()
    }

    companion object {
        private var dialogUtils: DialogUtils? = null

        @get:Synchronized
        val instance: DialogUtils?
            get() {
                if (dialogUtils == null) dialogUtils = DialogUtils()
                return dialogUtils
            }
    }
}